package cn.monkey.family.data.uc.role;

import cn.monkey.family.data.EntityMapper;
import cn.monkey.family.data.uc.resource.Resource;
import cn.monkey.family.data.uc.role.datascope.DataScopeDefinition;
import cn.monkey.family.data.uc.role.datascope.RoleDataScopeDefinitionRepository;
import cn.monkey.family.data.uc.role.resource.RoleResourcesRepository;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

import java.util.Collection;

@Mapper(unmappedTargetPolicy = org.mapstruct.ReportingPolicy.IGNORE)
public abstract class RoleMapper extends EntityMapper<RoleDto, Role, RoleVo> {

    @jakarta.annotation.Resource
    protected RoleResourcesRepository roleResourcesRepository;

    @jakarta.annotation.Resource
    protected RoleDataScopeDefinitionRepository roleDataScopeDefinitionRepository;

    @Named("getDataScopes")
    protected Collection<DataScopeDefinition> getDataScopes(String roleId) {
        return this.roleDataScopeDefinitionRepository.findByRoleId(roleId);
    }

    @Named("getResources")
    protected Collection<Resource> getResources(String roleId) {
        return this.roleResourcesRepository.findByRoleId(roleId);
    }

    @Override
    @Mapping(target = "dataScopes", source = "id", qualifiedByName = "getDataScopes")
    @Mapping(target = "resources", source = "id", qualifiedByName = "getResources")
    @Mapping(target = "creator", source = "creatorId", qualifiedByName = "getUserIdAndNamePairFromId")
    @Mapping(target = "updater", source = "updaterId", qualifiedByName = "getUserIdAndNamePairFromId")
    public abstract RoleVo toVo(Role role);


    @Override
    public abstract Role toEntity(RoleDto roleDto);
}
